<?php
// @formatter:off
/**
 * @file WCSession.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-08-05 00:26:28 +0000 (Mon, 05 Aug 2013) $
 *
 * $Id: WCSession.php 100 2013-08-05 00:26:28Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0.0.1/includes/security/WCSession.php $
 */
// @formatter:on

/**
 * @class WCSession This class represents a user session and all its information.
 */
class WCSession extends WCTableRepresentation {
	/**
	 * @var string[] This variable holds the list of columns that cannot be
	 * directly modified by the pseudo-method __set().
	 * In this case sess_id.
	 */
	protected $_PropertiesReadOnly = array("id");
	/**
	 * @var string This is the prefix of every column on the table
	 * '%sessions'.
	 */
	protected $_PropertiesPrefix = "sess_";
	/**
	 * @var string This is the name of the represented table. In this case,
	 * '%sessions'.
	 */
	protected $_PropertiesTable = "sessions";
	/**
	 * @var string This is a piece of query that will be used when a row of
	 * '%sessions' is updated.
	 */
	protected $_PropertiesAutoSet = ", sess_timestamp = sysdate()";
	//
	// Public methods.
	public function __set($name, $value) {
		global $wcDefaults;
		$ReadOnlyMode = $wcDefaults["read-only"];

		$wcDefaults["read-only"] = false;
		$out = parent::__set($name, $value);

		$wcDefaults["read-only"] = $ReadOnlyMode;

		return $out;
	}
	/**
	 * This is a redefinition of this loader to enforce some required
	 * values.
	 *
	 * @return boolean Returns final status of the object.
	 */
	public function load($id) {
		//
		// Calling inherited method.
		parent::load($id);
		//
		// If there's no issues with this session item, but it has no
		// salt, some is put on it
		if($this->ok() && !$this->salt) {
			//
			// Sadly, salt is just a random number.
			$this->salt = rand(1, 999999);
		}
		//
		// Returning final object status.
		return $this->ok();
	}
	/**
	 * This method allows to update a session id when it changes. This cases
	 * happen, for example, when a user closes a browser and a session is
	 * restored from cookies.
	 * After updating, it reloads this object with the session id.
	 *
	 * @param string $id PHP session id.
	 * @return boolean Returns final status of the object.
	 */
	public function setId($id) {
		//
		// Creating a statement to update session id.
		// @{
		$stmt = null;
		$stmtId = __CLASS__."::".__FUNCTION__;
		if($this->_db->has($stmtId)) {
			$stmt = $this->_db->get($stmtId);
		} else {
			$prefix = $this->_PropertiesPrefix;
			$table = $this->_dbprefix.$this->_PropertiesTable;

			$query = "update {$this->_dbprefix}{$table}\n";
			$query .= "set    {$prefix}id = :newid\n";
			$query .= "where  {$prefix}id = :currentid\n";

			$stmt = $this->_db->prepare($stmtId, $query);
		}
		// @}
		//
		// Building database parameters.
		$params = array(
			":newid" => $id,
			":currentid" => $this->id
		);
		//
		// Updating id.
		$stmt->execute($params);
		//
		// Reloading object.
		$this->load($id);
		//
		// Returning final object status.
		return $this->ok();
	}
}
?>